package ee.maoinvest.frontend.controller.command.regularreadingscommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.RegularReading;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.RegularReadingsService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.backend.services.implementations.RegularReadingsServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.regularreadingsforms.SaveElectricityMeterRegularReadingFormConverter;

public class FillSaveElectricityMeterRegularReadingFormWithInitialDataCommand implements Command
{
	private Logger logger = Logger.getLogger(FillSaveElectricityMeterRegularReadingFormWithInitialDataCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		SaveElectricityMeterRegularReadingFormConverter saveElectricityMeterRegularReadingFormConverter = null;
		RegularReadingsService regularReadingServiceImp = null;
		ElectricityMeterService electricityMeterServiceImpl = null;
		RegularReading latestRegularReading = null;
		List<ElectricityMeter> listOfAllElectricityMetersTakingPartInRegularReadings = null;
		
		try 
		{
			saveElectricityMeterRegularReadingFormConverter = new SaveElectricityMeterRegularReadingFormConverter();
			regularReadingServiceImp = new RegularReadingsServiceImpl();
			electricityMeterServiceImpl = new ElectricityMeterServiceImpl();
			listOfAllElectricityMetersTakingPartInRegularReadings = new ArrayList<ElectricityMeter>();
			
			latestRegularReading = regularReadingServiceImp.getLatestRegularReading();
			listOfAllElectricityMetersTakingPartInRegularReadings = electricityMeterServiceImpl.getListOfAllElectricityMetersTakingPartInRegularReadings();
			saveElectricityMeterRegularReadingFormConverter.fillWithInitialData(latestRegularReading, listOfAllElectricityMetersTakingPartInRegularReadings);
			request.setAttribute("saveElectricityMeterRegularReadingFormConverter", saveElectricityMeterRegularReadingFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("latestRegularReading", latestRegularReading);
			session.setAttribute("listOfAllElectricityMetersTakingPartInRegularReadings", listOfAllElectricityMetersTakingPartInRegularReadings);
			
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}
